<template>
  <div class="cart">
    <van-swipe-cell v-for="item in list" :key="item.id">
      <van-card
        :num="item.quantity"
        :price="item.unitPrice"
        :title="item.dish.name"
        :thumb="'http://8.138.102.145/images/' + item.dish.img"
      >
        <template #tags>
          <van-tag plain type="danger">{{
            item.taste ? item.taste.name : "无"
          }}</van-tag>
        </template>
        <template #footer>
          <span @click="inadd(item)">
            <v-icon name="hi-solid-minus-circle" fill="#ee0a24"></v-icon>
          </span>

          <span @click="add(item)">
            <v-icon name="hi-solid-plus-circle" fill="#ee0a24"></v-icon>
          </span>
        </template>
      </van-card>
      <template #right>
        <van-button
          @click="del(item)"
          square
          text="删除"
          type="danger"
          class="delete-button"
        />
      </template>
    </van-swipe-cell>
    <van-empty v-if="!list.length" image="search" description="空" />
    <div class="footer">
      <p>
        <span>合计：</span>
        <span class="price">￥{{ totalPrice }}</span>
      </p>
      <van-button class="btn" type="danger" @click="pay">付款</van-button>
    </div>
  </div>
</template>

<script>
export default {
  mounted() {
    this.get_list();
    console.log(this.$route);
  },
  methods: {
    get_list() {
      this.req.get("/cart", { params: { pageSize: 999 } }).then((res) => {
        this.list = res.rows;
        this.totalPrice = res.rows.reduce(
          (pre, cur) => pre + cur.unitPrice * cur.quantity,
          0
        );
      });
    },
    onSubmit() {},
    pay() {
      if (!this.list.length) {
        return;
      }
      this.$router.push({
        path: "/pay",
        query: {
          list: JSON.stringify(this.list),
        },
      });
    },
    del(item) {
      this.req.delete(`/cart?id=${item.id}`).then((res) => {
        this.get_list();
      });
    },
    inadd(item) {
      console.log(item);
      if (item.quantity == 1) {
      }
      this.req
        .put("/cart", {
          id: item.id,
          quantity: item.quantity - 1,
        })
        .then((res) => {
          this.get_list();
        });
    },
    add(item) {
      this.req
        .put("/cart", {
          id: item.id,
          quantity: item.quantity + 1,
        })
        .then((res) => {
          this.get_list();
        });
    },
  },
  data() {
    return {
      list: [],
      totalPrice: 0,
      index:true
    };
  },
};
</script>

<style lang="less" scoped>
.delete-button {
  height: 100%;
}
.cart {
  background-color: #fff;
  overflow: scroll;
  padding-bottom: 6.4375rem;
}
.footer {
  position: fixed;
  bottom: 49px;
  right: 0px;
  display: flex;
  align-items: center;
  justify-content: right;
  background-color: #fff;
  width: 100vw;
  p {
    margin-right: 1.25rem;
    .price {
      color: red;
    }
  }
  .btn {
    border-radius: 22px;
    width: 6.25rem;
  }
}
html {
  height: 100vh;
}
</style>
